<?php
class globalModel{
	public $db = null;
	public function __construct() {
		$this->db = &JFactory::getDBO();
	}

	public function getFirstImgURL($content) {
		$newStr = "";
		$signal = 'src=';
		$pos = strpos($content,$signal);
		$newStr = substr($content, $pos + 5);
		$pos = strpos($newStr,'"');
		$newStr = substr($newStr, 0, $pos);
		return $newStr;
	}
    
	public function getArticle($id) {
		$query = "select * from #__content where id = '{$id}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
    public function getArticleItem($id) {
        $query = "select * from #__content where id = '{$id}'";
        $this->db->setQuery( $query );
        $data = $this->db->loadAssoc();
        
        return $data;
    }
    
	public function getMWCItemId($catId) {
		$query = "select str_link from #__mwc_itemid where cat_id = '{$catId}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getArticleByCatId($catId, $contentId = null) {
		$query = "select * from #__content where catid = '{$catId}' ";
		if ( $contentId != null )
			$query .= " AND id <> ".$contentId;
		$query .= " ORDER BY ordering ASC LIMIT 0 , 2";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function deleteArticle($id) {
		$query = "delete from #__content where id = '{$id}'";
		return $this->db->execute($query);
	}
	
	public function getIntroduce()
	{
		$ar = $this->getArticle(49);
		$ar['introtext'] = $this->getShortText($ar['introtext'], 1000);
		return $ar;
	}
	
	public function getProductCategories()
	{
		$query = "select * from #__mwc_categories order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}	
	
	public function getCategoryName($id) {
		$query = "select * from #__vm_category where category_id = '{$id}'";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}
	
	public function getProducts()
	{
		$query = "SELECT * FROM #__vm_product ORDER BY product_id DESC LIMIT 0 , 2";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getMWCBanners($id = null)
	{
		$query = "SELECT * FROM #__mwc_banner ";
		if ( $id > 0 )
			$query .= " WHERE id = {$id} ";
		else
			$query .= " ORDER BY ordering"; // LIMIT 0,4
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getOptionsDetail($id)
	{
		$query = "select * from #__mwc_options_detail where option_id = '{$id}' order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}		
	
	public function getOptionsDetailFromId($id)
	{
		$query = "select * from #__mwc_options_detail where id = '{$id}' order by ordering ASC";
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas[0];
	}		
	
	public function getShortText($text, $startNum = 40) {
//		$text = strip_tags($text);
		$text = strip_tags($text,'<strong><br>');
		if(strlen($text) <= $startNum) {
			return $text;
		}
		$char = "p";
		while($char != " ") {
			$char = substr($text,$startNum, 1);
			$startNum++;
		}
		return substr($text,0, $startNum);
	}
	
	public function isHomePage()
	{
		$option = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		if ($option == 'com_content' && $view == 'frontpage') { 
			return true;
		} else {
			return false;
		}
	}
		
	// DK =============================
	public function getHomepageBanners()
	{
		$query = "SELECT ct.* FROM #__content AS ct";
			$query .= " WHERE ct.sectionid = 5 and ct.catid=34 and ct.state = 1";
			$query .= " ORDER BY ct.ordering LIMIT 0,3"; // LIMIT 0,4
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
    public function getHomepageBannersDefault()
    {
        $query = "SELECT ct.* FROM #__content AS ct";
            $query .= " WHERE ct.sectionid = 5 and ct.catid=60 and ct.state = 1";
            $query .= " ORDER BY ct.ordering LIMIT 0,10"; // LIMIT 0,4
        $this->db->setQuery( $query );
        $data = $this->db->loadAssocList();
        return $data;
    }
    
	public function get4Boxes()
	{
		$query = "SELECT ct.* FROM #__content AS ct";
			$query .= " WHERE ct.sectionid = 5 and ct.catid = 37 and ct.state = 1";
			$query .= " ORDER BY ct.ordering LIMIT 0,4"; // LIMIT 0,4
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
	
	public function getSubMenu($menutype)
	{
		$query = "SELECT ct.* FROM #__menu AS ct";
			$query .= " WHERE ct.menutype ='".$menutype."' and ct.published = 1";
			$query .= " ORDER BY ct.ordering"; // LIMIT 0,4
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();
		return $datas;
	}
    
    public function getChildrenMenu($menu_id, $state = -1) {
        $publish = $state != -1 ? ' AND m.published = ' . $state : '';
        
        $query = "SELECT m.* FROM #__menu AS m";
        $query .= " WHERE m.parent = '$menu_id'" . $publish;
        $query .= " ORDER BY m.ordering";
        
        $this->db->setQuery($query);         
        $data = $this->db->loadAssocList();
        
        return $data;
    }
    
    public function getMenu($menu_id, $published = 1) {
        $query = "SELECT ct.* FROM #__menu AS ct";
        $query .= " WHERE ct.menutype ='mainmenu' AND id = $menu_id AND ct.published = $published";
        
        $this->db->setQuery( $query );
        $data = $this->db->loadObject();
        
        return $data;
    }
    
    public function getContentItems($section_id, $category_id, $ignore){        
        $query = "SELECT c.* FROM #__content AS c";
        $query .= " WHERE c.sectionid = " . $section_id . " AND c.catid = " . $category_id . " AND c.id <> " . $ignore . " AND c.state = 1";
        $query .= " ORDER BY c.ordering"; // LIMIT 0,4
         
        $this->db->setQuery( $query );
        $data = $this->db->loadAssocList();  
        
        return $data;
    }
    
    public function getContentByCategory($category_id, $ignore){      
        if($category_id == 0){
            return array();    
        }
        
        $query = "SELECT c.* FROM #__content AS c";
        $query .= " WHERE c.catid = " . $category_id . " AND c.id <> " . $ignore . " AND c.state = 1";
        $query .= " ORDER BY c.ordering"; // LIMIT 0,4
         
        $this->db->setQuery( $query );
        $data = $this->db->loadAssocList();  
        
        return $data;
    }
    
	public function getCategoryOfSection($sectionID){
		$query = "SELECT ct.* FROM #__categories AS ct";
			$query .= " WHERE ct.section =".$sectionID." and ct.published = 1";
			$query .= " ORDER BY ct.ordering"; // LIMIT 0,4
		$this->db->setQuery( $query );
		$datas = $this->db->loadAssocList();  
		return $datas;
	}
    
    public function getLeftColumnArticle(){
        return $this->getArticle(57);     
    }
    
    public function getLeftColumnArticleItem(){
        return $this->getArticleItem(57);     
    }
    
    public function getLeftColumnArticleLink($id){
        $link = 'index.php?option=com_content&view=article&layout=articlefree&id=' . $id . '&Itemid=999';
        //$link = 'index.php?option=com_content&view=article&layout=articlefree&id=' . $id;
        return $link;     
    }
    
    public function getLeftColumnBookLink(){
        return 'index.php?option=com_user&view=book&Itemid=1000'; 
    }
   
    public function getArticleJobItem(){
        return $this->getArticleItem(106);
    }  
    
    public function loadSubmenus(){  
        $menus = &JSite::getMenu();                                             
        $menu  = $menus->getActive();             
        
        $sub_mns = $menus->getItems('parent', $menu->tree[0]);
        $sub_menus = array();

        if($menu->parent == 0){
            $sub_menus[] = $menu;         
        }
        else{
            $sub_menus[] = $menus->getItem($menu->parent);   
        }
        
        $sub_mns = $sub_mns ? $sub_mns : array();
        foreach($sub_mns as $mn){
            $sub_menus[] = $mn;    
        }  
        
        return $sub_menus;  
    }
    
    public function renderSubmenus($menu_id, $sec_id, $cat_id, $ignore, $show_layout = 0){
        $layout = $show_layout ? JRequest::getVar('layout', 0, 'get') : '';
        $emulate = JRequest::getVar('emulate', 0, 'get'); 
        $current_id = JRequest::getVar('id', 0, 'get');  

        $sub_menus = $this->loadSubmenus();
        $menus = &JSite::getMenu();                                             
        $menu  = $menus->getActive();
        
        $text = '<ul class="parent_menu">';
        foreach($sub_menus as $child_menu){
            if($child_menu->id == $menu->id){
                $parent_link = $emulate ? $menu->link : 'javascript:void(0);';
                $text .= '<li class="curr_left_menu">';
                //$text .= '<a href="javascript:void(0);">' . $menu->name . '</a>';
                $text .= '<a href="' . $parent_link . '">' . $menu->name . '</a>';
                $text .= '<ul class="child_menu">';
                
                $layout = $layout ? '&layout=' . $layout : '';
                if($sec_id){
                    $sub_menu = $this->getContentItems($sec_id, $cat_id, $ignore);
                }
                else{
                    $sub_menu = $this->getContentByCategory($cat_id, $ignore);
                }
                foreach($sub_menu as $key => $value ){                                    
                    //$selected = $current_id == $value['id'] ? ' class="active"'   : '';                                
                    $current  = $current_id == $value['id'] ? ' class="curr_sub"' : '';
                    $link = 'index.php?option=com_content&view=article&id=' . $value['id'] . '&category_id=' . $cat_id . '&section_id=' . $sec_id . '&Itemid=' . $menu_id . $layout . '&emulate=1';
                    
                    $text .= '<li>'; 
                    $text .= '<a ' . $current . 'href="' . $link . '">' . $value['title'] . '</a>';
                    $text .= '</li>'; 
                } 
                $text .= '</ul>';
                $text .= '</li>'; 
            }
            else{
                $text .= '<li class="curr_left_menu1">';
                $text .= '<a href="' . $child_menu->link . '&Itemid=' . $child_menu->id . '">' . $child_menu->name . '</a>';
                $text .= '</li>';
            }
        }
        $text .= '</ul>';
        
        return $text;
    }
    
    public function loadDeparments(){ 
        $data = null; 
        $query = "SELECT d.* FROM #__mwc_employee_dep AS d";
        $query .= " ORDER BY d.ordering"; 
     
        $this->db->setQuery($query);
        $data = $this->db->loadAssocList();  
        
        return $data;
    }    
    
    public function getDeparment($id){         
        $query = "SELECT * FROM #__mwc_employee_dep";
        $query .= " WHERE id = " .$id; 
     
        $this->db->setQuery($query);
        $data = $this->db->loadObject();  
        
        return $data;
    }    
}
